<template>
  <div style="padding:30px;">
    <div class="app-container">
      <div class="filter-container">
        <el-input v-model="querySn" placeholder="订单号" style="width: 200px; margin: 5px 0 5px 5px" class="filter-item" @keyup.enter.native="handleFilter"/>
        <el-input v-model="queryTradeOrderSn" placeholder="1688订单号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-select v-model="queryRefundStats" placeholder="状态" clearable style="width: 120px" class="filter-item">
          <el-option v-for="item in refundStatsOptions" :key="item" :label="item" :value="item" />
        </el-select>
        <el-select v-model="queryPayType" placeholder="支付类型" clearable style="width: 150px; margin: 5px 0 5px 0" class="filter-item">
          <el-option v-for="item in payTypeOptions" :key="item" :label="item" :value="item"/>
        </el-select>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter" />

        <!--        <el-button class="filter-item" style="margin: 5px 0 5px 0" type="primary" icon="el-icon-search" @click="handleFilter"/>-->
        <br>
      </div>
      <!--      列表页包含ID/商品图片/商品名称/商品价格/进货地址/商品订单号/买家收货信息/物流查询/状态/查询/筛选-->
      <el-table ref="multipleTable" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form  label-position="left" inline class="yl-table-expand">
              <el-form-item label="支付订单号">
                <span>{{ scope.row.sn }}</span>
              </el-form-item>
              <el-form-item label="拼团成功会员ID">
                <span>{{ scope.row.member_id }}</span>
              </el-form-item>
              <el-form-item label="收货人姓名">
                <span>{{ scope.row.address_info.name }}</span>
              </el-form-item>
              <el-form-item label="收货人电话">
                <span>{{ scope.row.address_info.mobile }}</span>
              </el-form-item>
              <el-form-item label="收货人地址">
                <span>{{ scope.row.address_info.province }}{{ scope.row.address_info.area }}{{ scope.row.address_info.street }}{{ scope.row.address_info.location }}</span>
              </el-form-item>
            </el-form>
            <el-form  label-position="left" inline class="yl-table-expand">
              <el-form-item label="拼团失败支付订单号">
                <span>{{ scope.row.fail_sn }}</span>
              </el-form-item>
              <el-form-item label="拼团失败会员">
                <span>{{ scope.row.fail_member_id }}</span>
              </el-form-item>

            </el-form>
          </template>
        </el-table-column>

        <el-table-column label="图片" width="120" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.product_info.img_url"  min-width="70" height="70" />
          </template>
        </el-table-column>
        <el-table-column label="1688订单号" width="220" align="center">
          <template slot-scope="scope">
            {{ scope.row.trade_order_sn }}
          </template>
        </el-table-column>
        <el-table-column label="产品编号" width="120" align="center">
          <template slot-scope="scope">
            {{ scope.row.product_id }}
          </template>
        </el-table-column>
        <el-table-column label="产品名称" width="150" align="center">
          <template slot-scope="scope">
            {{ scope.row.product_info.title }}
          </template>
        </el-table-column>
        <el-table-column label="消费金额" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.amount }}
          </template>
        </el-table-column>
        <el-table-column label="买家邮费" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.fee }}
          </template>
        </el-table-column>
        <el-table-column label="运费" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.post_fee }}
          </template>
        </el-table-column>
        <el-table-column label="成本" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.cost }}
          </template>
        </el-table-column>
        <el-table-column label="订单类型" width="80" align="center">
          <template slot-scope="{row}">
            <el-tag :type="row.type | typeCssFilter">
              {{ row.type | typeFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="110" align="center">
          <template slot-scope="{row}">
            <el-tag :type="row.refund_stats | refundStatsCssFilter">
              {{ row.refund_stats | refundStatsFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="支付" width="80" align="center">
          <template slot-scope="{row}">
            <el-tag :type="row.pay_type | payTypeCssFilter">
              {{ row.pay_type | payTypeFilter }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.created_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="退货时间" width="180" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.returned_at }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="150" class-name="small-padding fixed-width">
          <template slot-scope="{row,$index}">
            <el-button  type="primary" size="mini" @click="handleDetail(row)" >退货详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="currentPage" :limit.sync="pageSize" @pagination="fetchData({})"/>
      <el-dialog title="退货详情" :visible.sync="dialogFormVisible">
        <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="110px" style="width: 400px; margin-left:30px;">
          <el-form-item label="退款订单号" prop="name">
            <el-input v-model="temp.refund_order_id" disabled/>
          </el-form-item>
          <el-form-item label="退款原因" prop="name">
            <el-input v-model="temp.reason" disabled/>
          </el-form-item>
          <el-form-item label="卖家拒接信息" prop="name">
            <el-input v-model="temp.refuse" disabled/>
          </el-form-item>
          <el-form-item label="卖家姓名" prop="name">
            <el-input v-model="temp.seller_name" disabled/>
          </el-form-item>
          <el-form-item label="卖家电话" prop="name">
            <el-input v-model="temp.seller_mobile" disabled/>
          </el-form-item>
          <el-form-item label="卖家地址" prop="name">
            <el-input v-model="temp.seller_address" disabled/>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  import { getList, refundInfo } from '@/api/order'
  import Pagination from '@/components/Pagination'

  export default {
    components: { Pagination },
    filters: {
      statsFilter(stats) {
        const statsMap = {
          0: '待支付',
          1: '待发货',
          2: '已发货',
          3: '确认收货'
        }
        return statsMap[stats]
      },
      statsCssFilter(stats) {
        const statsMap = {
          0: 'danger',
          1: 'info',
          2: 'warn',
          3: 'success',
        }
        return statsMap[stats]
      },
      refundStatsFilter(refund_stats) {
        const refundstatsMap = {
          0: '不退款不退货',
          1: '发起退款',
          2: '发起退货',
          3: '退款成功',
          4: '退货成功',
          5: '退款失败',
          6: '退货失败',
          7: '等待买家发货',
          8: '买家已发货'
        }
        return refundstatsMap[refund_stats]
      },
      refundStatsCssFilter(refund_stats) {
        const refundstatsMap = {
          0: 'info',
          1: 'warn',
          2: 'warn',
          3: 'success',
          4: 'success',
          5: 'danger',
          6: 'danger',
          7: 'info',
          8: 'info'
        }
        return refundstatsMap[refund_stats]
      },
      rateStatsFilter(rate_stats) {
        const ratestatsMap = {
          0: '未评价',
          1: '已评价',
        }
        return ratestatsMap[rate_stats]
      },
      rateStatsCssFilter(rate_stats) {
        const ratestatsMap = {
          0: 'info',
          1: 'success',
        }
        return ratestatsMap[rate_stats]
      },
      typeFilter(type) {
        const typeMap = {
          1: '兑换',
          0: '购买'
        }
        return typeMap[type]
      },
      typeCssFilter(type) {
        const typeMap = {
          1: 'success',
          0: ''
        }
        return typeMap[type]
      },
      payTypeFilter(pay_type) {
        const typeMap = {
          0: '支付宝',
          1: '微信',
          2: '余额'
        }
        return typeMap[pay_type]
      },
      payTypeCssFilter(pay_type) {
        const typeMap = {
          0: 'info',
          1: 'warn',
          2: 'success',
        }
        return typeMap[pay_type]
      }

    },
    data() {
      return {
        hidden: false,
        listLoading: true,
        // 翻页相关数据 当前页，每页显示条数，排序方式，排序字段，符合条件的搜索结果数，搜索结果
        currentPage: 1,
        pageSize: 10,
        sort: 'desc',
        order: 'refunded_at',
        total: 0,
        orderStats: 2,//退货
        list: [],
        querySn: '',
        queryTradeOrderSn: '',
        queryType: '',
        queryPayType: '',
        queryStats: '',
        queryRefundStats:'',
        date: '',
        sortBy: '',
        type:0,
        // 搜索条件相关数据
        typeOptions: ['购买', '兑换'],
        statsOptions: ['待发货', '已发货', '确认发货'],
        payTypeOptions: ['支付宝', '微信', '余额'],
        refundStatsOptions: ['发起退货','退货成功', '退货失败','买家已发货','等待买家发货'],
        showReviewer: false,
        // 弹出层数据
        // 是否隐藏弹出层
        dialogFormVisible: false,
        // 临时数据
        temp: {
          id: undefined
        },
        // 上级分类数据
        parentArr: {},
        rules: {},
        // 下载的loading
        downloadLoading: false,
        downloadData: []
      }
    },
    mounted() {},
    created() {
      this.fetchData()
    },
    methods: {
      handleFilter() {
        this.currentPage = 1
        this.fetchData()
      },
      handleDownload() {
        this.downloadLoading = true
        this.downloadLoading = false
      },
      formatJson(filterVal) {
      },
      handleDetail(row) {
        console.log(row);
        refundInfo(row.sn).then(response => {
          const {data, meta} = response
          console.log(response)
          this.temp =data;
        }).catch(error => {
          this.listLoading = false
          this.list = []
          console.log('role get list error: ' + error)
        })

        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      fetchData() {
        this.listLoading = true
        const param = {
          sort: this.sort,
          order: this.order,
          page: this.currentPage,
          page_size: this.pageSize,
          order_stats:this.orderStats,
          type: this.type,
        }
        if (this.querySn !== '') {
          param.sn = this.querySn
        }
        if (this.queryTradeOrderSn !== '') {
          param.trade_order_sn = this.queryTradeOrderSn
        }

        if (this.queryStats !== '') {
          this.statsOptions.map((item, idx) => {
            if (this.queryStats === item) {
              param.stats = idx+1
            }
          })
        }
        if (this.queryRefundStats !== '') {
          this.refundStatsOptions.map((item, idx) => {
            if (this.queryRefundStats === item) {
              if(idx==4){
                param.refund_stats = 7
              }else{
                param.refund_stats = 2*idx+2

              }
            }
          })
        }
        if (this.queryType !== '') {
          this.typeOptions.map((item, idx) => {
            if (this.queryType === item) {
              param.type = idx
            }
          })
        }
        if (this.queryPayType !== '') {
          this.payTypeOptions.map((item, idx) => {
            if (this.queryPayType === item) {
              param.pay_type = idx
            }
          })
        }
        if (this.date !== '' && this.date !== null) {
          param.start_created_at = this.date[0].toLocaleDateString() + ' ' + this.date[0].toTimeString().split(' ')[0]
          param.end_created_at = this.date[1].toLocaleDateString() + ' ' + this.date[1].toTimeString().split(' ')[0]
        }
        console.log(param)
        getList(param).then(response => {
          const {data, meta} = response
          this.list = data
          this.listLoading = false
          this.currentPage = parseInt(meta.current_page)
          this.total = parseInt(meta.total)
        }).catch(() => {
          this.listLoading = false
          this.list = []
        })
      }
    }
  }
</script>

<style scoped>
  .filter-container {
    background: #fff;
    padding: 32px 16px;
  }
</style>
